let arr = [1, 2, 9, 4, 3, 8, 3, 7, 5];
let arr1 = [1, 2, 4, 1, 7, 8, 3];

function getMax(i) {
  if(i === 0) {
    return arr[0];
  }
  if(i === 1) {
    return Math.max(arr[0], arr[1]);
  }
  let A = getMax(i - 2) + arr[i];
  let B = getMax(i - 1);
  return Math.max(A, B);
}

function getMax2(arr) {
  let opts = new Array(arr.length);
  opts[0] = arr[0];
  opts[1] = Math.max(arr[0], arr[1]);
  for (let index = 2; index < arr.length; index++) {
    const element = arr[index];
    let A = opts[index - 2] + element;
    let B = opts[index - 1];
    opts[index] = Math.max(A, B);
  }
  return opts[arr.length - 1];
}

console.log(getMax2(arr));